<%@ page import="com.ljx.demo6.utils.DataBaseUtil" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="com.ljx.demo6.dao.NewsDao" %>
<%@ page import="com.ljx.demo6.domain.NewsInfo" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Objects" %>
<%@ page import="java.sql.SQLException" %>
<%@ page contentType="text/html; charset=gb2312" pageEncoding="UTF-8" %>
<%!
    public String codeToString(String str)
    {//处理中文字符串的函数
        String s=str;
        try
        {
            byte tempB[]=s.getBytes("ISO-8859-1");
            s=new String(tempB);
            return s;
        }
        catch(Exception e)
        {
            return s;
        }
    }
%>
<%
    String content = codeToString(request.getParameter("content"));
    String title = codeToString(request.getParameter("title"));
    String type = codeToString(request.getParameter("type"));
    if (Objects.nonNull(content) && Objects.nonNull(type) && Objects.nonNull(title)){
        try {
            boolean b = NewsDao.saveNews(new NewsInfo(title, content, type));
            if (b){
                out.println("<script type=\"text/javascript\">");
                out.println("alert('保存成功');");
                out.println("window.location.href='admin.jsp';");
                out.println("</script>");
            } else {
                out.println("<script type=\"text/javascript\">");
                out.println("alert('保存出错');");
                out.println("window.location.href='admin.jsp';");
                out.println("</script>");
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }
%>
<!DOCTYPE html>
<html>
<head>
    <title>JSP - Hello World</title>
    <style>
        .header{
            height: 55px;
            line-height: 55px;
            width: 100%;
            box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .05);
        }
        .header-main{
            width: 1200px;
            margin: 0 auto;
            display: flex;
            align-items: center;
        }
        .header div{
            padding: 0 10px;
            cursor: pointer;
        }
        .news{
            width: 800px;
            margin: 0 auto;
            margin-top: 10px;
        }
    </style>
</head>
<body>
<div class="header">
    <div class="header-main">
        <div><a href="index.jsp" style="text-decoration: none; color: black">首页</a></div>
        <div style="color: #409eff;">管理</div>
    </div>
</div>
<div style="display: flex;">
    <div class="news" style="width: 50%;">
        <%
            List<NewsInfo> newsAll = NewsDao.getNewsAll();
            int idx = 1;
            for (NewsInfo newsInfo : newsAll) {
        %>
        <table style="width: 100%; border-collapse: collapse; margin-top: 10px">
            <thead style="background-color: #f2f2f2;">
            <tr>
                <th colspan="2" style="padding: 10px; text-align: center; border-bottom: 2px solid #ccc;">
                    第<%=idx++%>个新闻
                </th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td style="width: 20%; padding: 10px; text-align: right; border-bottom: 1px solid #ccc;">标题：</td>
                <td style="padding: 10px; border-bottom: 1px solid #ccc;"><%=newsInfo.getTitle()%>
                </td>
            </tr>
            <tr>
                <td style="padding: 10px; text-align: right; border-bottom: 1px solid #ccc;">类型：</td>
                <td style="padding: 10px; border-bottom: 1px solid #ccc;"><%=newsInfo.getType()%>
                </td>
            </tr>
            <tr>
                <td style="padding: 10px; text-align: right; border-bottom: 1px solid #ccc;">内容：</td>
                <td style="padding: 10px; border-bottom: 1px solid #ccc;"><%=newsInfo.getContent()%>
                </td>
            </tr>
            <tr>
                <td style="padding: 10px; text-align: right; border-bottom: 1px solid #ccc;">发布时间：</td>
                <td style="padding: 10px; border-bottom: 1px solid #ccc;"><%=newsInfo.getCreateTime()%>
                </td>
            </tr>
            <tr>
                <td style="padding: 10px; text-align: right; border-bottom: 1px solid #ccc;">更新时间：</td>
                <td style="padding: 10px; border-bottom: 1px solid #ccc;"><%=newsInfo.getUpdateTime()%>
                </td>
            </tr>
            <tr>
                <td style="padding: 10px; text-align: right; border-bottom: 1px solid #ccc;">操作：</td>
                <td style="padding: 10px; border-bottom: 1px solid #ccc; display: flex">
                    <form method="POST" action="delete.jsp" style="margin-right: 10px">
                        <input name="id" value="<%=newsInfo.getId()%>" type="text" style="display: none;">
                        <button type="submit" class="delete-button">删除</button>
                    </form>
                    <form method="POST" action="update.jsp">
                        <input name="id" value="<%=newsInfo.getId()%>" type="text" style="display: none;">
                        <input name="title" value="<%=(newsInfo.getTitle())%>" type="text" style="display: none;">
                        <input name="content" value="<%=(newsInfo.getContent())%>" type="text" style="display: none;">
                        <input name="type" value="<%=(newsInfo.getType())%>" type="text" style="display: none;">
                        <input name="status" value="init" type="text" style="display: none;">
                        <button type="submit" class="delete-button">修改</button>
                    </form>
                </td>
            </tr>
            </tbody>
        </table>
        <%
            }
        %>
    </div>
    <div style="width: 40%; margin-top: 10px">
        <form method="POST" action="admin.jsp" name="form" onsubmit="return on_submit()">
            <div style="margin-bottom: 10px;">
                <label for="title" style="display: block; font-weight: bold;">标题：</label>
                <input id="title" placeholder="请输入标题" name="title" style="width: 50%; padding: 5px; border-radius: 3px;">
            </div>
            <div style="margin-bottom: 10px;">
                <label for="type" style="display: block; font-weight: bold;">类型：</label>
                <input id="type" placeholder="请输入类型" name="type" style="width: 50%; padding: 5px; border-radius: 3px;">
            </div>
            <div style="margin-bottom: 10px;">
                <label for="content" style="display: block; font-weight: bold;">内容：</label>
                <input id="content" placeholder="请输入内容" name="content" style="width: 50%; padding: 5px; border-radius: 3px;">
            </div>
            <div>
                <input type="submit" value="保存新闻" style="padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 3px; cursor: pointer;">
                <input type="reset" value="重新填写" style="padding: 10px 20px; background-color: red; color: white; border: none; border-radius: 3px; cursor: pointer;">
            </div>
        </form>
    </div>
</div>

</body>
</html>